// pages/other/login/login.js
import { toast } from '../../../../utils/Tools.js'
import { debounce,throttle ,debounces} from '../../../utils/Tools.js'
import { getLogin} from '../../../network/other.js'
const app = getApp()
Page({
  data: {
    userinfo:{
      username: '',
      password: ''
    },
    isDisabled: true,
    code: ''
  },
  onLoad: function (options) {
    wx.hideHomeButton()
    wx.clearStorageSync()
    // this.handleInput = debounce(this.handleInput,200)
    this.formSubmit = debounces(this.formSubmit,1500,true)
    // this.formSubmit = throttle(this.formSubmit,2000)
  },
  onShow(){
    wx.login({
      timeout: 500000,
      success: (res)=>{
        this.setData({code: res.code})
      }
    })
    clearInterval(app.globalData.driverTask)
    
  },
  business(){
    wx.reLaunch({
      url:'/pages/other/register/register'
    })
  },
  //         函数事件         //
  // handleInput(e){
  //   console.log(e)
  //   e.detail.value ? this.setData({ isDisabled: false }) : this.setData({ isDisabled: true })
  // },
  handleInput(e){
    // console.log(e)
    let userinfo=this.data.userinfo;
    let value=e.target.dataset.value;
    userinfo[value]=e.detail.value;
    this.setData({
      userinfo
    })
    e.detail.value ? this.setData({ isDisabled: false }) : this.setData({ isDisabled: true })
    wx.login({
      success: (res)=>{
        this.setData({code: res.code})
      }
    })
  },
  //      网络请求      //
  formSubmit(e){
    clearInterval(app.globalData.driverTask)
    
    const {username,password} = this.data.userinfo
    const data = {
      name: username,
      password,
      code: this.data.code
    }
    wx.requestSubscribeMessage({
      tmplIds: ['R0zgiGQObzSjDFxH3KQjp_wRs1EkFI-l7iegE2Ru678'],
      success: async res=>{
        if(/^1[3456789]\d{9}$/.test(data.name) && data.name.replace(/^\s+|\s+$/gm,'')){
          const res = await getLogin(data)
          if(res.code === 200){
            wx.setStorageSync('token', res.token)
            wx.setStorageSync('login_status','driver')
            wx.setStorageSync('driver_user', res.data)
            if(wx.getStorageSync('token')){
              toast('登录成功','success')
              setTimeout(async ()=>{
                wx.reLaunch({
                  url: '/driver_side/pages/order-hall/index/index'
                });
                //   分配任务
                const {work_status} = wx.getStorageSync('driver_user');
                app.globalData.driverStatus = work_status
                if(work_status === 0){
                  app._task()
                }
              },1500)
            }
          }else{
            toast(res.msg)
          }
        }else{
          toast('手机号码错误,请重新输入')
        }
      }
    })
  }
  // async formSubmit(){
  //   clearInterval(app.globalData.driverTask)
  //   const {username,password} = this.data.userinfo;
  //   const data = {
  //     name: username,
  //     password,
  //     code: this.data.code
  //   }
  //   if(/^1[3456789]\d{9}$/.test(data.name) && data.name.replace(/^\s+|\s+$/gm,'')){
  //     const res = await getLogin(data);
  //     if(res.code === 200){
  //       wx.setStorageSync('token', res.token)
  //       wx.setStorageSync('login_status','driver')
  //       wx.setStorageSync('driver_user', res.data)
  //       if(wx.getStorageSync('token')){
  //         wx.requestSubscribeMessage({
  //           tmplIds: ['R0zgiGQObzSjDFxH3KQjp_wRs1EkFI-l7iegE2Ru678'],
  //           success:res=>{
  //             setTimeout(async ()=>{
  //               wx.reLaunch({
  //                 url: '/driver_side/pages/order-hall/index/index'
  //               });
  //               //   分配任务
  //               const {work_status} = wx.getStorageSync('driver_user');
  //               app.globalData.driverStatus = work_status
  //               if(work_status === 0){
  //                 app._task()
  //               }
  //             },1500)
  //           },
  //           fail:err=>{
  //             setTimeout(async ()=>{
  //               wx.reLaunch({
  //                 url: '/driver_side/pages/order-hall/index/index'
  //               });
  //               //   分配任务
  //               const {work_status} = wx.getStorageSync('driver_user');
  //               app.globalData.driverStatus = work_status
  //               if(work_status === 0){
  //                 app._task()
  //               }
  //             },1500)
  //           }
  //         })
  //           toast('登录成功','success')
              
  //       }
  //     }else{
  //       toast(res.msg)
  //     }
  //   }else{
  //     toast('手机号码错误,请重新输入')
  //   }
  // },
})
